/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;

import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.TemplateableElement;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * Dialog that displays association in the model, so the user can select one. 
 * It is useful for example for connector types (connectors are typed by associations)
 */
public class AssociationTreeSelectionDialog extends TypedElementTreeSelectionDialog{

	/** selected association  */
	protected Association association;
	
	/**
	 * Creates a new AssociationTreeSelectionDialog
	 * @param model the top package of the model (profile, package or model)
	 * @param parent the parent shell
	 */
	public AssociationTreeSelectionDialog(Shell parent, Package model){
		super(parent, model);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.TypedElementTreeSelectionDialog#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Association Selection");
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.TypedElementTreeSelectionDialog#setSelection(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	protected void setSelection(SelectionEvent e) {
		Widget selectedWidget = e.item;
		if(selectedWidget.getData() instanceof Association) {
			association = (Association)selectedWidget.getData();
		} else {
			// sets association to null, if selected element is not an association.
			// erase a previous association
			// example: select association, the a package
			// if association is not set to null, it keeps the association A1 as result
			// whereas the package is selected
			// TODO should improve the dialog: Ok should not be enabled when association is null
			// TODO should improve the dialog: selection should be initialized depending on 
			// the original type( ex open  a window on an already typed connector)
			association = null;
		}
	}

	/**
	 * This method builds the element tree widget containing all packageable elements of a given package.
	 * @param _package the package to explore
	 * @param elemTree tree parent of packageable elements
	 */
	protected void buildImportTreeList(TreeItem elemTree, Package _package){
		Iterator<PackageableElement> elemIter = _package.getPackagedElements().iterator();
		while(elemIter.hasNext()) {
			Element elem = (Element) elemIter.next();
			if(elem instanceof Package){
				TreeItem item = new TreeItem(elemTree, SWT.NONE);
				String name = ((NamedElement)elem).getName();
				item.setText((name!=null)?name:"<undefined>");
				item.setData(elem);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(elem));
				buildImportTreeList(item, (Package)elem);
			}
			else if(elem instanceof Association){
				TreeItem item = new TreeItem(elemTree, SWT.NONE);
				String name = ((NamedElement)elem).getName();
				item.setText((name!=null)?name:"<undefined>");
				item.setData(elem);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(elem));
				if (elem instanceof TemplateableElement) {
					TemplateableElement templateableElement = (TemplateableElement)elem ;
					if (templateableElement.isTemplate()) {
						buildTemplateTreeList(item, templateableElement);
					}
				}
			} 
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.TypedElementTreeSelectionDialog#buildTemplateTreeList(org.eclipse.swt.widgets.TreeItem, org.eclipse.uml2.uml.TemplateableElement)
	 */
	@Override
	protected void buildTemplateTreeList(TreeItem elemTree, TemplateableElement templateableElement){
		com.cea.papyrus.umlutils.TemplateSignature templateSignatureUtil = new com.cea.papyrus.umlutils.TemplateSignature(templateableElement.getOwnedTemplateSignature()) ;
		Iterator<com.cea.papyrus.umlutils.Type> ite= templateSignatureUtil.getAccessibleTypes().iterator();
		while(ite.hasNext()){
			Element element=((com.cea.papyrus.umlutils.Type)ite.next()).getUml2Element();
			if(element instanceof Association) {
				TreeItem item = new TreeItem(elemTree, SWT.NONE);
				item.setText(((NamedElement)element).getName());
				item.setData(element);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(element));
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.TypedElementTreeSelectionDialog#getResult()
	 */
	@Override
	public Association getResult(){
		return association;
	}
}
